package com.ds.dao;

import com.ds.config.DataSourceConfig;
import com.ds.model.BuildingInfo;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BuildingDao {
    private QueryRunner queryRunner = new QueryRunner(DataSourceConfig.getDataSource());
    public int saveBuilding(BuildingInfo buildingInfo) throws SQLException {
        return  queryRunner.update("INSERT INTO building_info (building_area,using_area,layer,arch, start_time,end_time,order_num,create_time,delete_flag) VALUES (?,?,?,?,?,?,?,?,?)"
                ,buildingInfo.getBuildingArea(),buildingInfo.getUsingArea(),buildingInfo.getLayer(),buildingInfo.getArch(),
                buildingInfo.getStartTime(),buildingInfo.getEndTime(),buildingInfo.getOrderNum(),buildingInfo.getCreateTime(),buildingInfo.getDeleteFlag());
    }

    public int getTotal() throws SQLException {
        ScalarHandler<Long> longHandler = new ScalarHandler<>();
        return Math.toIntExact(queryRunner.query("SELECT COUNT(*) FROM building_info", longHandler));
    }

    //方法一
    //public List<BuildingInfo> getBuildingList(int i, int limit) throws SQLException {
    //    return queryRunner.query("SELECT building_id as buildingId,building_area as buildingArea,using_area as usingArea,layer,arch,start_time as startTime,end_time as endTime,order_num as orderNum,create_time as createTime,update_time as updateTime,delete_flag as deleteFlag FROM building_info Order By  order_num LIMIT ?,?", new BeanListHandler<>(BuildingInfo.class), i, limit);
    //}
    public List<BuildingInfo> getBuildingList(int i, int limit) throws SQLException {
        return queryRunner.query("SELECT * FROM building_info Order By  order_num LIMIT ?,?", resultSet -> {

                List<BuildingInfo> list = new ArrayList<>();
                while (resultSet.next()) {
                    BuildingInfo bi = new BuildingInfo();
                    bi.setBuildingId(resultSet.getInt("building_id"));
                    bi.setBuildingArea(resultSet.getDouble("building_area"));
                    bi.setUsingArea(resultSet.getDouble("using_area"));
                    bi.setLayer(resultSet.getInt("layer"));
                    bi.setArch(resultSet.getString("arch"));
                    bi.setStartTime(resultSet.getDate("start_time"));
                    bi.setEndTime(resultSet.getDate("end_time"));
                    bi.setOrderNum(resultSet.getInt("order_num"));
                    bi.setUpdateTime(resultSet.getDate("update_time"));
                    bi.setDeleteFlag(resultSet.getInt("delete_flag"));
                    bi.setCreateTime(resultSet.getDate("create_time"));
                    list.add(bi);
                }

                return list;
            }, i, limit);
    }

    public int deleteBuilding(List<Integer> buildingIds) throws SQLException {
        StringBuilder sql = new StringBuilder("delete from building_info where building_id in (");
        Object[] params = new Object[buildingIds.size()];
        for (int b = 0; b < buildingIds.size(); b++) {
            if (b == buildingIds.size()- 1){
                sql.append("?)");
            }else {
                sql.append("?,");
            }
        }
        return queryRunner.update(sql.toString(), params);
    }
}
